using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace MasterPageProj.Features.Feature1
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("6d669c58-69a2-4e58-afc9-77b93c6249c5")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        private void SetMasterPage(SPWeb web, string masterPage)
        {
            var masterUri = new Uri(web.Url + "/_catalogs/masterpage/" + masterPage);

            web.MasterUrl = masterUri.AbsolutePath;
            web.CustomMasterUrl = masterUri.AbsolutePath;
            web.Update();
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            var parentSite = properties.Feature.Parent as SPSite;
            if (parentSite != null)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (var site = new SPSite(parentSite.RootWeb.Url))
                    {
                        using (var web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;
                            SetMasterPage(web, "v5.master");
                        }
                    }
                });
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            var parentSite = properties.Feature.Parent as SPSite;
            if (parentSite != null)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (var site = new SPSite(parentSite.RootWeb.Url))
                    {
                        using (var web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;
                            SetMasterPage(web, "V4.master");
                        }
                    }
                });
            }
        }


        // Uncomment the method below to handle the event raised after a feature has been activated.

        //public override void FeatureActivated(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
